package com.yuanfengerp.Analysis.service.impl;

import com.yuanfengerp.Analysis.dao.IDepartmentDao;
import com.yuanfengerp.Analysis.model.Department;
import com.yuanfengerp.Analysis.service.IDepartmentService;
import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.publicUtil.SnowflakeIdWorker;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private IDepartmentDao departmentDao;

    @Override
    public ArrayList<Department> getDepartmentList(Integer pageSize, Integer offset) {
        ArrayList<Department> list = departmentDao.getDepartmentList(pageSize , offset);
        return list;
    }
    @Override
    public Result<Department> insertDepartment(Department department) {
//        if(department.getDepartment_Number() == null){
//            return Result.failure(400,"部门编号不能为空");
//        }
//        if(department.getDepartment_Name() == null){
//            return Result.failure(400,"部门名称不能为空");
//        }
//        if(department.getAbbreviation() == null){
//            return Result.failure(400,"部门简称不能为空");
//        }
//        if(department.getParent_Department() == null){
//           department.setParent_Department(null);
//        }
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1);
        department.setDepartment_Number(idWorker.nextId());
       int dt = departmentDao.addDepartment(department);
        return dt == 1 ? Result.success(department) : Result.failure(400,"添加失败");
    }

    @Override
    public ArrayList<Department> getDepartment(String department_Number) {
        return departmentDao.getDepartment(department_Number);
    }

    @Override
    public Result<Department> updateDepartment(Department department) {
        int dt = departmentDao.updateDepartment(department);
        return dt > 0 ? Result.success(department) : Result.failure(400,"更新失败");
    }
}
